package com._520it.crm.service.impl;

import com._520it.crm.domain.ClientExploitation;
import com._520it.crm.domain.Employee;
import com._520it.crm.mapper.ClientExploitationMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.ClientExploitationQueryObject;
import com._520it.crm.service.IClientExploitationService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/14.
 */
@Service
public class ClientExploitationServiceImpl implements IClientExploitationService {
    @Autowired
    private ClientExploitationMapper clientExploitationMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return clientExploitationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ClientExploitation record) {
        record.setCreatetime(new Date());//设置创建时间
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();
        record.setCreate(current);//设置创建人
        record.setStatus(false);//设置实施状态为未实施
        record.setExecutetime(null);//设置实施时间
        record.setExecuteresult(null);//设置实施效果
        return clientExploitationMapper.insert(record);
    }

    @Override
    public ClientExploitation selectByPrimaryKey(Long id) {
        return clientExploitationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ClientExploitation> selectAll() {
        return clientExploitationMapper.selectAll();
    }

    @Override
    public int updateByPrimaryKey(ClientExploitation record) {
        return clientExploitationMapper.updateByPrimaryKey(record);
    }

    @Override
    public PageResult queryPage(ClientExploitationQueryObject qo) {
        Long count = clientExploitationMapper.queryPageCount(qo);
        if (count <= 0) {
            return new PageResult(0L, Collections.EMPTY_LIST);
        }
        List<ClientExploitation> result = clientExploitationMapper.queryPageDataResult(qo);
        PageResult pageResult = new PageResult(count, result);
        return pageResult;
    }

    @Override
    public void executePlan(ClientExploitation ce) {
        clientExploitationMapper.executePlan(ce.getId(),ce.getExecutetime(),ce.getExecuteresult());
    }
}
